package com.deep.flink;

import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.flink.types.Row;

import java.time.Duration;

/**
 * @author sw
 * @create 2023-06-30 11:26
 */
public class Test2 {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        SingleOutputStreamOperator<Event> eventStream = env.addSource(new ClickSource())
                .assignTimestampsAndWatermarks(WatermarkStrategy.<Event>forBoundedOutOfOrderness(Duration.ZERO)
                        .withTimestampAssigner(new SerializableTimestampAssigner<Event>() {
                            @Override
                            public long extractTimestamp(Event event, long l) {
                                return event.getTimestamp();
                            }
                        }));


        //
        StreamTableEnvironment tableEnv = StreamTableEnvironment.create(env);

        // daststream -> table
        Table eventTable = tableEnv.fromDataStream(eventStream);

        //

        Table resTable = eventTable.select("user,url,age").where("age > 2");
        //
        resTable.printSchema();
        //
//        Table fetch = resTable.fetch(0);
        DataStream<Row> rowDataStream = tableEnv.toDataStream(resTable);
        rowDataStream.print("result");


        env.execute();
    }
}
